package com.xr.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xr.mapper.MemberMapper;
import com.xr.mapper.OrderMapper;
import com.xr.mapper.SetMealMapper;
import com.xr.pojo.MemberExample;
import com.xr.pojo.OrderExample;
import com.xr.service.ReportService;
import com.xr.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = ReportService.class)
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private SetMealMapper setMealMapper;

    @Override
    public Map<String, Object> getBusinessReportData() {
        Map<String, Object> map = new HashMap<>();
//         * reportDate: null,//报表日期
//         * todayNewMember: 0,//新增会员数
//         * totalMember: 0,//总会员数
//         * thisWeekNewMember: 0,//本周新增会员
//         * thisMonthNewMember: 0,//本月新增会员
//         * todayOrderNumber: 0,//今日预约数
//         * todayVisitsNumber: 0,//今日到诊数
//         * thisWeekOrderNumber: 0,//本周预约数
//         * thisWeekVisitsNumber: 0,//本周到诊数
//         * thisMonthOrderNumber: 0,//本月预约数
//         * thisMonthVisitsNumber: 0,//本月到诊数
        /**
         * reportData: {
         * reportDate: null,//报表日期
         * todayNewMember: 0,//新增会员数
         * totalMember: 0,//总会员数
         * thisWeekNewMember: 0,//本周新增会员
         * thisMonthNewMember: 0,//本月新增会员
         * todayOrderNumber: 0,//今日预约数
         * todayVisitsNumber: 0,//今日到诊数
         * thisWeekOrderNumber: 0,//本周预约数
         * thisWeekVisitsNumber: 0,//本周到诊数
         * thisMonthOrderNumber: 0,//本月预约数
         * thisMonthVisitsNumber: 0,//本月到诊数
         * hotSetmeal: []
         * }
         *
         */
        //报表日期
        Date today = DateUtils.getToday();
        map.put("reportDate",today);

        //新增会员数
        MemberExample memberExample = new MemberExample();
        MemberExample.Criteria criteria = memberExample.createCriteria();
        criteria.andRegtimeEqualTo(today);
        long todayNewMember = memberMapper.countByExample(memberExample);
        map.put("todayNewMember",todayNewMember);

        //总会员数
        long totalMember = memberMapper.countByExample(null);
        map.put("totalMember",totalMember);

        //本周新增会员
        MemberExample memberExample2 = new MemberExample();
        MemberExample.Criteria criteria2 = memberExample2.createCriteria();
        criteria2.andRegtimeGreaterThanOrEqualTo(DateUtils.getFirstDayOfWeek(today));
        long thisWeekNewMember = memberMapper.countByExample(memberExample2);
        map.put("thisWeekNewMember",thisWeekNewMember);

        //本月新增会员
        MemberExample memberExample3 = new MemberExample();
        MemberExample.Criteria criteria3 = memberExample3.createCriteria();
        criteria3.andRegtimeGreaterThanOrEqualTo(DateUtils.getFirstDay4ThisMonth());
        long thisMonthNewMember = memberMapper.countByExample(memberExample3);
        map.put("thisMonthNewMember",thisMonthNewMember);

        //今日预约数
        OrderExample orderExample = new OrderExample();
        OrderExample.Criteria orderCriteria1 = orderExample.createCriteria();
        orderCriteria1.andOrderdateEqualTo(today);
        long todayOrderNumber = orderMapper.countByExample(orderExample);
        map.put("todayOrderNumber",todayOrderNumber);
        //今日到诊数
        orderCriteria1.andOrderstatusEqualTo("已到诊");
        long todayVisitsNumber = orderMapper.countByExample(orderExample);
        map.put("todayVisitsNumber",todayVisitsNumber);
        //本周预约数
        OrderExample orderExample2 = new OrderExample();
        OrderExample.Criteria orderCriteria2 = orderExample.createCriteria();
        orderCriteria2.andOrderdateGreaterThanOrEqualTo(DateUtils.getFirstDayOfWeek(today));
        long thisWeekOrderNumber = orderMapper.countByExample(orderExample2);
        map.put("thisWeekOrderNumber",thisWeekOrderNumber);
        //本周到诊数
        orderCriteria2.andOrderstatusEqualTo("已到诊");
        long thisWeekVisitsNumber = orderMapper.countByExample(orderExample2);
        map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        //本月预约数
        OrderExample orderExample3 = new OrderExample();
        OrderExample.Criteria orderCriteria3 = orderExample.createCriteria();
        orderCriteria3.andOrderdateGreaterThanOrEqualTo(DateUtils.getFirstDay4ThisMonth());
        long thisMonthOrderNumber = orderMapper.countByExample(orderExample3);
        map.put("thisMonthOrderNumber",thisMonthOrderNumber);
        //本月到诊数
        orderCriteria3.andOrderstatusEqualTo("已到诊");
        long thisMonthVisitsNumber = orderMapper.countByExample(orderExample3);
        map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);

        //热门套餐
        List<Map<String, Object>> hotSetMeal = setMealMapper.findHotSetMeal();
        map.put("hotSetmeal",hotSetMeal);
        return map;
    }
}
